/**
 * 
 */
package com.haisen.lee.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * @author Haisen Lee
 * 
 */
public class WriteJavaScriptFile {

	/**
	 * 
	 */
	public WriteJavaScriptFile() {
		super();
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentContent
	 * @param fileName
	 */
	public void writeJavaScriptFile(String documentContent, String fileName) {
		this.writeJavaScriptFile(documentContent, fileName, "");
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentContent
	 * @param filePath
	 * @param fileName
	 */
	public void writeJavaScriptFile(String documentContent, String filePath,
			String fileName) {
		this.writeJavaScriptFile(documentContent, this.getClass()
				.getClassLoader(), fileName, filePath);
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentContent
	 * @param filePath
	 * @param fileName
	 * @param charset
	 */
	public void writeJavaScriptFile(String documentContent, String filePath,
			String fileName, Charset charset) {
		this.writeJavaScriptFile(documentContent, this.getClass()
				.getClassLoader(), fileName, filePath, charset);
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentContent
	 * @param classLoader
	 * @param filePath
	 * @param fileName
	 */
	public void writeJavaScriptFile(String documentContent,
			ClassLoader classLoader, String filePath, String fileName) {
		this.writeJavaScriptFile(documentContent, filePath, fileName, Charset
				.defaultCharset());
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentContent
	 * @param classLoader
	 * @param fileName
	 * @param filePath
	 * @param charset
	 */
	public void writeJavaScriptFile(String documentContent,
			ClassLoader classLoader, String fileName, String filePath,
			Charset charset) {
		OutputStream os = null;
		try {
			os = new BufferedOutputStream(new FileOutputStream(classLoader
					.getResource(null).getPath()
					+ File.pathSeparator
					+ filePath
					+ File.pathSeparator
					+ fileName));
			byte[] buffer = new byte[1024];
			do {
				if (documentContent.length() > 1024) {
					buffer = documentContent.substring(0, 1024).getBytes(
							charset);
					documentContent = documentContent.substring(1024,
							documentContent.length());
				} else {
					buffer = documentContent.substring(0,
							documentContent.length()).getBytes(charset);
					documentContent = "";
				}
				os.write(buffer, 0, buffer.length);
			} while (documentContent.length() > 0);
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentURL
	 * @param fileName
	 */
	public void writeJavaScriptFile(URL documentURL, String fileName) {
		this.writeJavaScriptFile(documentURL, fileName, "");
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentURL
	 * @param fileName
	 * @param filePath
	 */
	public void writeJavaScriptFile(URL documentURL, String fileName,
			String filePath) {
		this.writeJavaScriptFile(documentURL, this.getClass().getClassLoader(),
				fileName, filePath);
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-5-15
	 * 
	 * @param documentURL
	 * @param classLoader
	 * @param fileName
	 * @param filePath
	 */
	public void writeJavaScriptFile(URL documentURL, ClassLoader classLoader,
			String fileName, String filePath) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new BufferedInputStream(documentURL.openStream());
			os = new BufferedOutputStream(new FileOutputStream(classLoader
					.getResource(null).getPath()
					+ File.pathSeparator
					+ filePath
					+ File.pathSeparator
					+ fileName));
			byte[] buffer = new byte[1024];
			while (is.read(buffer) > 0) {
				os.write(buffer, 0, buffer.length);
			}
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
